package com.temp.controller.user;

import com.temp.dto.SearchDonationDTO;
import com.temp.entity.User;
import com.temp.result.Result;
import com.temp.service.DonationService;
import com.temp.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/donation")
@Api(tags = "捐献相关接口")
@Slf4j
@Validated
public class DonationCollector {
    @Autowired
    private DonationService donationService;

    @PostMapping("/query")
    @ApiOperation("获取用户的捐款记录")
    @RequiresPermissions("user:donation:query")
    public Result getDonations(@Valid @RequestBody SearchDonationDTO donationDTO) {
        User user = ShiroUtils.getUser();
        donationDTO.formatParam();
        donationDTO.setUserId(user.getId());
        return Result.success(donationService.findAllUserDonation(donationDTO),"apiMsg.querySuccess");
    }

    @PostMapping("/queryById")
    @ApiOperation("获取id对应的活动")
    @RequiresPermissions("user:donation:queryById")
    public Result getByCampaignId(@Valid @RequestBody SearchDonationDTO donationDTO) {
        donationDTO.formatParam();
        return Result.success(donationService.findAllByCampaign(donationDTO),"apiMsg.querySuccess");
    }

    @GetMapping("/queryStById")
    @ApiOperation("获取id对应的统计信息")
    @RequiresPermissions("user:donation:queryStById")
    public Result getStByCampaignId(@RequestParam Long campaignId) {
        return Result.success(donationService.countDonationDate(campaignId),"apiMsg.querySuccess");
    }

    @GetMapping("/myself")
    @ApiOperation("获取自己的捐款情况")
    @RequiresPermissions("user:donation:myself")
    public Result countOneSelfDonations() {
        User user = ShiroUtils.getUser();
        return Result.success(donationService.countOneSelfDonations(user.getId()), "apiMsg.querySuccess");
    }
}
